package com.kooison.idevice4u.database;

import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

public class EmailLogService {
	public void create(EmailLog emailLog) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(emailLog);
		} finally {
			pm.close();
		}
	}

	public EmailLog retrieve(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			EmailLog emailLog = pm.getObjectById(EmailLog.class, id);
			pm.detachCopy(emailLog);
			return emailLog;
		} finally {
			pm.close();
		}
	}


	
	public List<EmailLog> retrieve(String id, String product, String region) {
		// default channel is ONLINE
		return retrieve(id, product, region, EmailLog.CHANNEL.ONLINE);
	}
	
	public List<EmailLog> retrieve(String id, String product, String region, EmailLog.CHANNEL channel) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(EmailLog.class);
		query.setFilter("email == emailParam && product == productParam && region == regionParam");
		query.declareParameters("String emailParam, String productParam, String regionParam");

		try {
			List<EmailLog> results = (List<EmailLog>) query.execute(id, product, region);
			pm.detachCopyAll(results);
			return results;
		} finally {
			query.closeAll();
			pm.close();
		}
	}
	
	public List<EmailLog> retrieve(String product, String region) {
		// default channel is ONLINE
		return retrieve(product, region, EmailLog.CHANNEL.ONLINE);
	}

	public List<EmailLog> retrieve(String product, String region, EmailLog.CHANNEL channel) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(EmailLog.class);
		query.setFilter("product == productParam && region == regionParam && channel == channelParam");
		query.declareParameters("String productParam, String regionParam, String channelParam");

		try {
			List<EmailLog> results = (List<EmailLog>) query.execute(product, region, channel.toString());
			pm.detachCopyAll(results);
			return results;
		} finally {
			query.closeAll();
			pm.close();
		}
	}
	
	public void update(EmailLog emailLog) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			EmailLog original = pm.getObjectById(EmailLog.class, emailLog.getId());
			original.setEmail(emailLog.getEmail());
			original.setProduct(emailLog.getProduct());
			original.setRegion(emailLog.getRegion());
			original.setSentMailCount(emailLog.getSentMailCount());
			original.setLastModified(emailLog.getLastModified());
			original.setProductStatus(emailLog.getProductStatus());
			original.setChannel(emailLog.getChannel());
			pm.makePersistent(original);
		} finally {
			pm.close();
		}
	}

	public void delete(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			EmailLog emailLog = pm.getObjectById(EmailLog.class, id);
			pm.deletePersistent(emailLog);
		} finally {
			pm.close();
		}
	}

}
